import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class Img {


    @Test
    public void test10() throws IOException {
        for (int i = 1; i <= 10; i++) {
            String url = "https://bing.ioliu.cn/?p="+i;
            print("Fetching %s...", url);

            Document doc = Jsoup.connect(url).get();
            //获取后缀名为 jpg和png的图片元素集合
            //Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g)]");
            Elements a = doc.select(".progressive");
            for (Element element : a) {
                Elements a1 = element.select(".calendar");
                Elements b1 = element.select(".location");
                Elements c1 = element.select(".download");
                String imageUrl = c1.attr("abs:href");
                //photo/WYBaldEagle_EN-AU3747996768?force=download"
                // http://h1.ioliu.cn/bing/YosemiteBridge_ZH-CN10163806053_1920x1080.jpg
                System.out.println("图片名：" + imageUrl.substring(28, imageUrl.length() - 15));
                System.out.println("下载地址：" + imageUrl);
                String url2 = (imageUrl.substring(28, imageUrl.length() - 15));
                String imageUrl1 = "http://h1.ioliu.cn/bing/" + url2 + "_1920x1080.jpg";

                URL url1 = new URL(imageUrl1);
                URLConnection connection = url1.openConnection();
                connection.setRequestProperty("User-Agent", "Mozilla/5.0");
                InputStream in = connection.getInputStream();


                String path = "D:\\test";

                String imageName = a1.text() + b1.text() + ".jpg";

                OutputStream out = new FileOutputStream(new File(path, imageName));
                byte[] buff = new byte[1024];
                int j = 0;
                while ((j = in.read(buff)) != -1) {
                    out.write(buff, 0, j);
                }
                in.close();
                out.close();
            }

            System.out.println("爬取第"+i+"页图片完毕");
        }
    }

    private static void print(String msg, Object... args) {

        System.out.println(String.format(msg, args));
    }

    @Test//DJQ原创，如有雷同，纯属巧合
    public void test2() throws IOException {
        Document document = Jsoup.connect("http://www.shicimingju.com/book/index.html").get();
        Elements a = document.select("h2");
        Elements b = a.select("a[href]");
        int j=1;
        for (Element element : b) {
            String text = element.text();//书名
            File file = new File("D:\\test\\" + text);
            if (!file.exists()) {
                file.mkdir();
            }
            String a1 = element.attr("abs:href");
            Document document1 = Jsoup.connect(a1).timeout(1000000000).get();
            Elements b1 = document1.select(".book-mulu");
            Elements c1 = b1.select("a[href]");

            System.out.println("正在书写《"+text+"》......"+j);
            for (Element element1 : c1) {

                String text1 = element1.text();//每一回名
                File file1 = new File(file + "\\" + text1 + ".txt");
                if (!file1.exists()) {
                    file1.createNewFile();
                }
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file1, true)));

                String a11 = element1.attr("abs:href");
                Document document2 = Jsoup.connect(a11).timeout(1000000000).get();
                Elements b11 = document2.select(".chapter_content");
                String text2 = b11.text();//文章内容

                out.write(text2 + "\r\n");
                out.close();


            }
            System.out.println("书写结束");
            j++;
        }
    }






}
